package com.teufandsub.socialproximity.gui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.teufandsub.socialproximity.R;
import com.teufandsub.socialproximity.controller.Constants;
import com.teufandsub.socialproximity.controller.Controller;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * Home Activity of our application, where you can access to all the functionalities of it.
 */

public class HomeActivity extends SherlockActivity implements OnClickListener {
	
	private Controller controller;
	private static boolean firstLaunch=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_home);
		
		controller = Controller.getInstance();
		ImageButton bt_find_friend = (ImageButton) findViewById(R.id.button_find_friend);
		bt_find_friend.setOnClickListener(this);
		
		ImageButton bt_friend_list = (ImageButton) findViewById(R.id.button_friend_list);
		bt_friend_list.setOnClickListener(this);
		
		ImageButton bt_event_chat = (ImageButton) findViewById(R.id.button_event_chat);
		bt_event_chat.setOnClickListener(this);
		
		ImageButton bt_profil = (ImageButton) findViewById(R.id.button_profil);
		bt_profil.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(!firstLaunch){
			controller.checkSession(this, HomeActivity.class);
			firstLaunch=true;
		}
		
	}
	
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		MenuInflater inflater = this.getSupportMenuInflater();
		inflater.inflate(R.menu.home_menu, menu);
		return true;
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch(item.getItemId()){
			case R.id.action_close:
				intent = new Intent(this, CloseActivity.class);
				startActivity(intent);
				break;
			case R.id.action_close_button:
				intent = new Intent(this, CloseActivity.class);
				startActivity(intent);
				break;
			case R.id.action_log_out: 
				controller.disconnect(this);
				break;
			default:;
		}
		return true;
	}
	
	@Override
	public void onClick(View v) {
		Intent i;
		switch(v.getId()){
			case R.id.button_find_friend:
				i = new Intent(this, ResearchFriendActivity.class);
				startActivity(i);
				break;
				
			case R.id.button_friend_list:
				i = new Intent(this, FriendListActivity.class);
				startActivity(i);
				break;
				
			case R.id.button_event_chat:
				Toast.makeText(HomeActivity.this, getResources().getString(R.string.comingsoon), Toast.LENGTH_SHORT).show();
				break;
				
			case R.id.button_profil:
				
				new AsyncTask<Void, Void, Void>() {
					ProgressDialog dialog;
					Intent i = new Intent(HomeActivity.this, SettingsActivity.class);
					@Override
					protected void onPostExecute(Void result) {
						super.onPostExecute(result);
						dialog.dismiss();
					}
					
					@Override
					protected Void doInBackground(Void... params) {
						startActivity(i);
						return null;
					}
					
					protected void onPreExecute() {
						dialog = ProgressDialog.show(HomeActivity.this, "", getResources().getString(R.string.loading), true);
					};
					
				}.execute();
				break;
				
			default: Log.e(Constants.APP_NAME, "No action on this view");
		}	
	}
	
	@Override
	public void onBackPressed() {
		controller.checkSession(this, HomeActivity.class);
	}
}
